package com.i2863.watchdog.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.i2863.watchdog.util.HttpSessionUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuweiwei on 2017/4/17.
 */
public class SmsUtil {
    public static final String url = "http://api01.monyun.cn:7901/sms/v2/std/single_send";
    public static final String key = "a80d8ec760e0621266c513f9f5d9f617";
    public static final CloseableHttpClient client = HttpSessionUtils.buildHttpClient();
    private static final Logger logger = LoggerFactory.getLogger(SmsUtil.class);


    /**
     * 给单个手机发送一条短信
     * @param mobis 单个手机号
     * @param strMessage  短信内容
     * @throws IOException
     */
    public static void sendMsg(String mobis,String strMessage) throws IOException {
        Map<String,String> param = new HashMap<String,String>();
        param.put("apikey",key);
        param.put("mobile",mobis);
        param.put("content", strMessage);
        String json = HttpSessionUtils.httpPost(client,url,param,"GBK","UTF-8");
        JSONObject result = JSON.parseObject(json);
        int code = result.getInteger("result");
        if(code!=0){
            SmsCodeType type = SmsCodeType.valueOf(code);
            throw new SmsException(type);
        }else{
            Long  msgid = result.getLong("msgid");
            logger.info("发送短信成功,手机:{},平台流水号:{}",mobis,msgid);
        }
    }

}
